package com.wantup.demo01;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Zhaoluowei
 * @version 1.0
 * @date 2019/6/25 11:24
 */

/**
 * 原子变量 ：j.u.c.atomic
 * 1.内部封装的变量属性由 volatile 修饰
 * 2.CAS算法保证原子性。
 * 计算机底层的支持。
 */
public class TestAtomicDemo {

    public static void main(String[] args) {
        AtomicDemo atomicDemo = new AtomicDemo();
        for (int i = 0; i < 10; i++) {
            new Thread(atomicDemo).start();
        }

    }

}

//@Data
//@AllArgsConstructor
//@NoArgsConstructor
//@Accessors(chain = true)
class AtomicDemo implements Runnable {

    //    private int num = 0;
    //是一个包装类
    private AtomicInteger num = new AtomicInteger();


    @Override
    public void run() {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " : " + getNum());

    }

    public int getNum() {
        //return num++;
        return num.getAndIncrement();
    }


}

